package com.wsoft.im.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.ImRedisConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.im.entity.ImGroupMemberEntity;
import com.wsoft.im.mapper.ImGroupMemberMapper;
import com.wsoft.im.util.GroupMemberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2025/1/14 09:29
 */
@Service
public class ImGroupMemberManager extends ServiceImpl<ImGroupMemberMapper, ImGroupMemberEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 新增群成员
     * @param groupId
     * @param memberIdList
     * @return
     */
    public boolean addMember(Long groupId,List<Long> memberIdList){
        //已存在成员列表
        List<ImGroupMemberEntity> existMemberList = getMemberList(groupId);
        if(CollUtil.isNotEmpty(existMemberList)){
            List<Long> existMemberIdList = existMemberList.stream().map(ImGroupMemberEntity::getMemberId).collect(Collectors.toList());
            memberIdList = memberIdList.stream().filter(memberId -> !existMemberIdList.contains(memberId)).collect(Collectors.toList());
        }

        List<ImGroupMemberEntity> groupMemberList = GroupMemberUtil.buildGroupMember(groupId, memberIdList,false);
        boolean b = super.saveBatch(groupMemberList);
        if(b){
            redisService.del(ImRedisConstant.GROUP_MEMBER_LIST + groupId);
        }
        return b;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ImGroupMemberEntity getByGroupId(Long groupId,Long memberId){
        QueryWrapper<ImGroupMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImGroupMemberEntity::getGroupId,groupId)
                .eq(ImGroupMemberEntity::getMemberId,null == memberId ? LoginUserUtil.getUserInfo().getLoginUserVO().getId() : memberId);
        return getOne(query);
    }

    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(ImGroupMemberEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(ImRedisConstant.GROUP_MEMBER_LIST + entity.getGroupId());
        }
        return b;
    }

    /**
     * 移除群成员
     * @param groupId
     * @param memberIdList
     * @return
     */
    public boolean removeMember(Long groupId,List<Long> memberIdList){
        QueryWrapper<ImGroupMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImGroupMemberEntity::getGroupId,groupId)
                .in(ImGroupMemberEntity::getMemberId,memberIdList);
        boolean b = super.remove(query);
        if(b){
            redisService.del(ImRedisConstant.GROUP_MEMBER_LIST + groupId);
        }
        return b;
    }


    /**
     * 获取群主
     * @param groupId
     * @return
     */
    public ImGroupMemberEntity getGroupLeader(Long groupId){
        List<ImGroupMemberEntity> memberList = getMemberList(groupId);
        List<ImGroupMemberEntity> collect = memberList.stream().filter(item -> item.getIsGroupLeader() == 1).collect(Collectors.toList());
        if(CollUtil.isNotEmpty(collect)){
            return collect.get(0);
        }
        return null;
    }

    /**
     * 判断群组中是否存在该成员
     * @param groupId
     * @param memberId
     * @return
     */
    public boolean isMember(Long groupId,Long memberId){
        List<ImGroupMemberEntity> memberList = this.getMemberList(groupId);
        if(CollUtil.isEmpty(memberList)){
            return false;
        }
        List<Long> memberIdList = memberList.stream().map(ImGroupMemberEntity::getMemberId).collect(Collectors.toList());
        return memberIdList.contains(memberId);
    }

    /**
     * 移除群组下 所有成员
     * @param groupId
     * @return
     */
    public boolean removeAllMember(Long groupId){
        QueryWrapper<ImGroupMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImGroupMemberEntity::getGroupId,groupId);
        boolean b = remove(query);
        if(b){
            redisService.del(ImRedisConstant.GROUP_MEMBER_LIST + groupId);
        }
        return b;
    }

    /**
     * 获取组下人员信息
     * @param groupId
     * @return
     */
    @GetRedisCache(type = ImRedisConstant.GROUP_MEMBER_LIST,
            expire_sec = RedisKeyConstant.Cms.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<ImGroupMemberEntity> getMemberList(Long groupId){
        QueryWrapper<ImGroupMemberEntity> query = new QueryWrapper<>();
        query.lambda().eq(ImGroupMemberEntity::getGroupId,groupId);
        return list(query);
    }

}
